//
//  ComplicationController.swift
//  NJU QR WatchKit Extension
//
//  Created by 许煜恒 on 8/11.
//

import ClockKit


class ComplicationController: NSObject, CLKComplicationDataSource {
    
    // MARK: - Complication Configuration

    func getComplicationDescriptors(handler: @escaping ([CLKComplicationDescriptor]) -> Void) {
        let descriptors = [
            CLKComplicationDescriptor(identifier: "launcher", displayName: "Show QR", supportedFamilies: CLKComplicationFamily.allCases)
            // Multiple complication support can be added here with more descriptors
        ]
        
        // Call the handler with the currently supported complication descriptors
        handler(descriptors)
    }
    
    func handleSharedComplicationDescriptors(_ complicationDescriptors: [CLKComplicationDescriptor]) {
        // Do any necessary work to support these newly shared complication descriptors
    }

    // MARK: - Timeline Configuration
    
    func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
        // Call the handler with the last entry date you can currently provide or nil if you can't support future timelines
        handler(nil)
    }
    
    func getPrivacyBehavior(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
        // Call the handler with your desired behavior when the device is locked
        handler(.showOnLockScreen)
    }

    // MARK: - Timeline Population
    
    func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
        // Call the handler with the current timeline entry
        let icon=UIImage(systemName: "qrcode")!.withRenderingMode(.alwaysTemplate).resizableImage(withCapInsets: .zero, resizingMode: .stretch)
        let iconProvider=CLKImageProvider(onePieceImage: icon)
        let fcIconProvider=CLKFullColorImageProvider(fullColorImage: icon)
        var entry:CLKComplicationTimelineEntry?
        switch complication.family {
        case .circularSmall:
            let t=CLKComplicationTemplateCircularSmallSimpleImage(imageProvider: iconProvider)
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        case .extraLarge:
            let t=CLKComplicationTemplateExtraLargeSimpleImage(imageProvider: iconProvider)
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        case .modularSmall:
            let t=CLKComplicationTemplateModularSmallSimpleImage(imageProvider: iconProvider)
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        case .modularLarge:
            let t=CLKComplicationTemplateModularLargeTallBody(
                headerTextProvider: CLKTextProvider(format: "Tap to show your"),
                bodyTextProvider: CLKTextProvider(format: "QR Code")
            )
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        case .utilitarianLarge:
            let t=CLKComplicationTemplateUtilitarianLargeFlat(
                textProvider: CLKTextProvider(format: "Tap to open NJU QR"),
                imageProvider: iconProvider
            )
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        case .utilitarianSmall:
            let t=CLKComplicationTemplateUtilitarianSmallSquare(imageProvider: iconProvider)
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        case .utilitarianSmallFlat:
            let t=CLKComplicationTemplateUtilitarianSmallFlat(
                textProvider: CLKTextProvider(format: "Tap to open NJU QR"),
                imageProvider: iconProvider
            )
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        case .graphicCorner:
            let t=CLKComplicationTemplateGraphicCornerCircularImage(imageProvider: fcIconProvider)
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        case .graphicCircular:
            let t=CLKComplicationTemplateGraphicCircularImage(imageProvider: fcIconProvider)
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        case .graphicBezel:
            let t=CLKComplicationTemplateGraphicBezelCircularText(
                circularTemplate: CLKComplicationTemplateGraphicCircularImage(imageProvider: fcIconProvider)
            )
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        case .graphicRectangular:
            let t=CLKComplicationTemplateGraphicRectangularStandardBody(
                headerTextProvider: CLKTextProvider(format: "Tap to open"),
                body1TextProvider: CLKTextProvider(format: "NJU QR")
            )
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        case .graphicExtraLarge:
            let t=CLKComplicationTemplateGraphicExtraLargeCircularImage(imageProvider: fcIconProvider)
            entry=CLKComplicationTimelineEntry(date: Date(), complicationTemplate: t)
        @unknown default:
            print("*** Unknown Complication Family: \(complication.family) ***")
            return
        }
        
        handler(entry)
    }
    
    func getTimelineEntries(for complication: CLKComplication, after date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
        // Call the handler with the timeline entries after the given date
        handler(nil)
    }

    // MARK: - Sample Templates
    
    func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
        // This method will be called once per supported complication, and the results will be cached
        handler(nil)
    }
}
